﻿using System;
using System.Collections.Generic;
using System.Text;
using EBookReader.Library;
using EBookReader.Library.Data;
using EBookReader.Library.Config;
using EBookReader.Library.Encoder;

namespace EBookReader.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //PageInfo page = new PageInfo();
            //page.Host = "www.zzu.edu.cn";
            //page.Port = 80;
            //page.Url = "/";
            //PageInfo gzPage = new PageInfo();
            //gzPage.Host = "www.qidian.com";
            //gzPage.Port = 80;
            //gzPage.Url = "/";
            //SimpleWebReader reader = new SimpleWebReader();
            //GZipWebReader gzReader = new GZipWebReader();
            //reader.Page = page;
            //reader.Read += ShowInfo;
            //gzReader.Page = gzPage;
            //gzReader.Read += ShowInfo;
            //try
            //{
            //    //reader.Encode = "utf-8";
            //    reader.Connect();
            //    Console.Write("Length:");
            //    Console.WriteLine(reader["Content-Length"]);
            //    System.IO.StreamWriter sw = new System.IO.StreamWriter("E:\\result.txt", false, Encoding.GetEncoding("GB2312"));
            //    sw.Write(reader.Page.Buffer);
            //    sw.Close();
            //    Console.ReadKey();
            //    gzReader.Connect();
            //    sw = new System.IO.StreamWriter("E:\\result2.txt", false, Encoding.GetEncoding("GB2312"));
            //    sw.Write(gzReader.Page.Buffer);
            //    sw.Close();
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
            EBookWebsite website = new EBookWebsite();
            website.Name = "翠微居";
            website.Encode = "GB2312";
            website.Intro = "翠微居小说";
            website.Type = "none";
            website.Url = "http://www.cuiweiju.com/";
            website.InfoRegex = "<div\\s*id\\s*=\\s*\\\"title\\\"\\s*>\\s*<h1>\\s*(.*?)</h1>\\s*</div>\\s*<div\\s*id\\s*=\\s*\\\"info\\\">(.*?)</div>";
            website.TitleRegex = "<td\\s*.*?class=.*?>\\s*<a\\s*href\\s*=\\s*\\\"(.*?)\\\"\\s*>(.*?)</a\\s*>";
            website.ContentRegex = "<div\\s*id\\s*=\\s*\\\"title\\\"\\s*>\\s*<h2>(.*?)</h2>\\s*</div>\\s*<div\\s*id\\s*=\\s*\\\"info\\\"\\s*>.*?</div>\\s*<div\\s*id\\s*=\\s*\\\"content\\\"\\s*>(.*?)<center>";
            //website.ContentEncodeRegex.Add("<font\\s*color=.*?>.*?</font><br>\\s*<font\\s*color=.*?>.*?</font>");
            System.IO.FileStream fs = new System.IO.FileStream("E:\\jjwxc.xml", System.IO.FileMode.OpenOrCreate);
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(EBookWebsite));
            xs.Serialize(fs, website);
            fs.Close();
            //EBookWebsite jjwxc;
            //System.IO.FileStream fs = new System.IO.FileStream("E:\\jjwxc.xml", System.IO.FileMode.Open);
            //System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(EBookWebsite));
            //jjwxc = xs.Deserialize(fs) as EBookWebsite;
            //fs.Close();
            //PageInfo page = new PageInfo();
            //page.Host = "www.jjwxc.net";
            //page.Url = "/onebook.php?novelid=262136";
            //TitleEncoder titleEncoder = new TitleEncoder();
            //titleEncoder.Page = page;
            //titleEncoder.Website = jjwxc;
            //titleEncoder.Read += ShowInfo;
            //titleEncoder.Connect();
            //titleEncoder.Translate();
            //Console.WriteLine("-------------------------------");
            //Console.WriteLine(string.Format("Title:{0}", titleEncoder.BookInfo.Title));
            //Console.WriteLine(string.Format("Author:{0}", titleEncoder.BookInfo.Author));
            //foreach (BookChapter bc in titleEncoder.BookInfo.Chapters)
            //{
            //    Console.WriteLine(string.Format("ChapterTitle:{0}", bc.Title));
            //    Console.WriteLine(string.Format("ChapterURL:{0}", bc.Url));
            //}
            //Console.WriteLine("-------------------------------");
            //Console.ReadKey();
            //ChapterEncoder ce = new ChapterEncoder(titleEncoder.BookInfo.Chapters.Dequeue().Url);
            //ce.Website = jjwxc;
            //ce.Connect();
            //ce.Translate();
            //Console.WriteLine(string.Format("Title:{0}", ce.Chapter.Title));
            //Console.WriteLine(string.Format("Content:{0}", ce.Chapter.Content));
            //Console.ReadKey();
        }

        public static void ShowInfo(PageInfo info)
        {
            Console.WriteLine(string.Format("Content-Length:{0},Recevied:{1}", info.Length, info.Recevied));
        }
    }
}
